package cn.stylefeng.guns.modular.rm;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description TODO
 * @Author xiesm
 * @Date 2024/4/17 9:15
 **/
public class FBTest {

    public static void main(String[] args) {
        showGameName();
//        getGameInfos();
    }

    public static String getGameInfos() {
        List<String> ids = FileUtil.readLines("D:\\home\\rm\\list.txt", Charset.defaultCharset());
        for (int i = 0; i < ids.size(); i++) {
//        for (int i = 0; i < 1; i++) {
            String id = ids.get(i);
            String body = getGames("1", id, null);
//            String body = FileUtil.readString("D:\\乱七八糟\\foot.txt", Charset.defaultCharset());
            getGameInfo(body);
        }
        return null;
    }

    public static String getGameInfo(String data) {
        JSONObject jsonObject = JSONObject.parseObject(data);
        jsonObject = jsonObject.getJSONObject("data");
        String text = "{}\t{}";

        Date date = new Date(jsonObject.getLong("bt"));

        String dateStr = DateUtil.format(date, "MM-dd HH:mm");

        JSONObject lg = jsonObject.getJSONObject("lg");
        JSONArray ts = jsonObject.getJSONArray("ts");
        System.out.println(StrUtil.format(text,
                lg.getString("na"),
                dateStr
        ));
        System.out.println(StrUtil.format(text,
                ts.getJSONObject(0).getString("na"),
                ts.getJSONObject(1).getString("na")
        ));

        JSONArray recodes = jsonObject.getJSONArray("mg");
        for (int i = 0; i < recodes.size(); i++) {
            JSONObject recode = recodes.getJSONObject(i);
            if (StrUtil.equals("波胆", recode.getString("nm"))) {
                JSONArray ops = recode.getJSONArray("mks").getJSONObject(0).getJSONArray("op");
                for (int j = 0; j < ops.size(); j++) {
                    JSONObject op = ops.getJSONObject(j);
                    String nm = op.getString("nm");
                    if (Arrays.asList("0-3", "3-3", "3-0").contains(nm)) {
                        System.out.println(StrUtil.format(text,
                                nm,
                                op.getString("od")
                        ));
                    }
                }
            }
            if (StrUtil.equals("波胆-上半场", recode.getString("nm"))) {
                JSONArray ops = recode.getJSONArray("mks").getJSONObject(0).getJSONArray("op");
                for (int j = 0; j < ops.size(); j++) {
                    JSONObject op = ops.getJSONObject(j);
                    String nm = op.getString("nm");
                    if (Arrays.asList("2-2").contains(nm)) {
                        System.out.println(StrUtil.format(text,
                                nm,
                                op.getString("od")
                        ));
                    }
                }
            }
            if (StrUtil.equals("单/双", recode.getString("nm"))) {
                JSONArray ops = recode.getJSONArray("mks").getJSONObject(0).getJSONArray("op");
                String dan = "";
                String suan = "";
                dan = ops.getJSONObject(0).getString("od");
                suan = ops.getJSONObject(1).getString("od");
//                for (int j = 0; j < ops.size(); j++) {
//                    JSONObject op = ops.getJSONObject(j);
//                    String nm = op.getString("nm");
//                    if (Arrays.asList("单", "双").contains(nm)) {
//                        dan
//                        System.out.println(StrUtil.format(text,
//                                nm,
//                                op.getString("od")
//                        ));
//                    }
//                    if (Arrays.asList("单", "双").contains(nm)) {
//                        dan
//                        System.out.println(StrUtil.format(text,
//                                nm,
//                                op.getString("od")
//                        ));
//                    }
//                }
                System.out.println(StrUtil.format("单/双:\t{}\t{}",
                        dan,
                        suan
                ));
            }
        }
        return null;
    }

    public static void showGameName() {
        for (int i = 0; i < 10; i++) {
            String body = getGames("0", null, i + 1);
            getGame(body);
            JSONObject data = JSONObject.parseObject(body);
            data = data.getJSONObject("data");
            Integer total = data.getInteger("total");
            Integer size = data.getInteger("size");
            if ((i == 0 ? 1 : i) * size > total) {
                break;
            }
        }
    }

    public static String getGame(String data) {
        JSONObject jsonObject = JSONObject.parseObject(data);
        String text = "{}\t{}\t{}\t{}\t{}";

        JSONArray recodes = jsonObject.getJSONObject("data").getJSONArray("records");
        for (int i = 0; i < recodes.size(); i++) {
            JSONObject recode = recodes.getJSONObject(i);
            JSONArray ops = recode.getJSONArray("mg")
                    .getJSONObject(0)
                    .getJSONArray("mks")
                    .getJSONObject(0)
                    .getJSONArray("op");
            String teamA = ops.getJSONObject(0).getString("na");
            String teamB = ops.getJSONObject(1).getString("na");

            String league = recode.getJSONObject("lg").getString("na");
            String date = DateUtil.format(new Date(recode.getLong("bt")), "yyyy-MM-dd HH:mm:ss");
            String id = recode.getString("id");

            System.out.println(StrUtil.format(text, id, league, date, teamA, teamB));

        }
        return null;
    }

    public static String getGames(String type, String gameid, Integer current) {
        String url = null;
        switch (type) {
            case "0":
                url = "http://124.222.75.100:8089/cp/fbmarket?type=0&current=" + current;
                break;
            case "1":
                url = StrUtil.format("http://124.222.75.100:8089/cp/fbmarket?type=1&gameid={}", gameid);
                break;
        }

        String body = HttpUtil.createGet(url)
                .execute().body();
        return body;
    }
}
